<template>
  <div :class="['login-input', active ? 'active' : '', err ? 'err' : '']">
    <input
      class="login-input__input"
      :placeholder="placeholder"
      :type="currentType"
      :value="modelValue"
      :maxlength="20"
      @focus="onFocus"
      @blur="onBlur"
      @input="onInput"
    />
    <img v-if="err" class="login-input__icon" src="@/assets/icons/desktop/warning.svg" alt="" >
    <div v-else-if="type === 'password'">
      <img v-if="currentType === 'password'" class="login-input__icon opacity-40" src="@/assets/icons/desktop/eye-slash.svg" alt="" @click="currentType = 'text'">
      <img v-else class="login-input__icon opacity-40" src="@/assets/icons/desktop/eye.svg" alt="" @click="currentType = 'password'" >
    </div>
  </div>
</template>

<script lang="ts" setup>
import { onMounted, ref } from 'vue';

interface IProps {
  modelValue: string
  type: string
  err?: string
  placeholder?: string
}
const props = withDefaults(defineProps<IProps>(), {
  modelValue: '',
  type: '',
  err: '',
  placeholder: '',
});

const emits = defineEmits(['update:modelValue'])

const active = ref(false)
const currentType = ref('')

onMounted(() => {
  currentType.value = props.type
})
const onFocus = () => {
  active.value = true
}
const onBlur = () => {
  active.value = false
}
const onInput = (e: any) => {
  emits('update:modelValue', e.target.value)
}
</script>

<style lang="scss">
.login-input {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 348px;
  height: 41px;
  background: linear-gradient(90deg, rgba(58, 87, 228, 0.6) 29.32%, rgba(59, 87, 228, 0.24) 84.91%);
  border-radius: 4px;
  padding: 0 12px;
  box-sizing: border-box;
  border: 1px solid transparent;

  &__icon {
    width: 14px;
    height: 14px;
  }
  &__input {
    flex: 1;
    color: #fff;
    font-size: 12px;
    line-height: 40px;
    background:none;
    outline:none;
    border:none;
    &:focus {
      border:none;
    }
  }
  &:hover, &.active {
    border: 1px solid #41BFE8;
    cursor: pointer;
  }
  input::-webkit-input-placeholder { /* WebKit browsers 适配谷歌 */
    color: rgba($color: #fff, $alpha: 0.4);
  }
  &.err {
    background: linear-gradient(90deg, rgba(214, 221, 255, 0.6) 29.32%, rgba(214, 221, 255, 0.24) 84.91%);
    backdrop-filter: blur(11px);
  }
}
</style>